// pages/sell/sell.js
var products = require('list.js')
Page({
  tab1: function() {
    this.setData({
      bool: 1
    })
  },
  tab2: function() {
    var arr_sofa = []
    var lists = this.data.lists
    lists.forEach(function (item, index) {
      if (item.state == 1) {
        arr_sofa.push(item)
      }
    })
    this.setData({
      sofa: arr_sofa
    })
    this.setData({
      bool: 2
    })
  },
  tab3: function() {
    var arr_chest = []
    var lists = this.data.lists
    lists.forEach(function (item, index) {
      if (item.state == 2) {
        arr_chest.push(item)
      }
    })
    this.setData({
      chest: arr_chest
    })
    this.setData({
      bool: 3
    })
  },
  tab4: function() {
    var arr_tea_table = []
    var lists = this.data.lists
    lists.forEach(function (item, index) {
      if (item.state == 3) {
        arr_tea_table.push(item)
      }
    })
    this.setData({
      tea_table: arr_tea_table
    })
    this.setData({
      bool: 4
    })
  },
  tab5: function() {
    var arr_chair = []
    var lists = this.data.lists
    lists.forEach(function(item, index) {
      if (item.state == 4) {
        arr_chair.push(item)
      }
    })
    this.setData({
      chair: arr_chair
    })
    this.setData({
      bool: 5
    })
  },
  go_search: () => {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  go_to_detail: function(e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/details/details?id=' + id,
    })
  },
  go_buy: function() {
    this.setData({
      show_buy: true
    })
  },
  go_sell: function() {
    this.setData({
      show_buy: false
    })
  },
  submit: function() {
    var that = this
    var arr = []
    var storage = wx.getStorageSync('products')

    //设置id
    var id = storage.length + 1
    var photo = that.data.photo
    photo[0].id = id
    photo[0].sales = 0
    photo[0].join = 0
    that.setData({
      photo
    })
    arr = storage
    arr.unshift(photo[0])

    var lists = that.data.lists
    lists = arr
    that.setData({
      lists
    })

    wx.setStorage({
      key: 'products',
      data: lists,
    })
    this.setData({
      show_buy: true
    })
  },
  //商品图片
  chooseImage: function() {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: 'original',
      sourceType: 'album',
      success: function(res) {
        var obj = {
          picture: res.tempFilePaths[0],
          detail: res.tempFilePaths[0]
        }
        that.setData({
          photo: [obj]
        })
      }
    })
  },
  //商品标题
  title: function(e) {
    var photo = this.data.photo
    photo[0].name = e.detail.value
    this.setData({
      photo
    })

  },
  //商品描述
  describe: function(e) {
    var photo = this.data.photo
    photo[0].describe = e.detail.value
    this.setData({
      photo
    })
  },
  //商品价格
  price: function(e) {
    var photo = this.data.photo
    photo[0].price = e.detail.value
    this.setData({
      photo
    })
  },
  //选择商品类别
  kind: function(e) {
    var photo = this.data.photo
    photo[0].state = e.detail.value
    this.setData({
      photo
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    bool: 1,
    lists: [],
    chair: [],
    tea_table: [],
    //photo[]要添加的商品
    photo: [],
    chest: [],
    sofa: [],
    show_buy: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      lists: products.products
    })
    wx.setStorage({
      key: 'products',
      data: products.products,
    })


    // var arr_chair = [],
    //   arr_tea_table = [],
    //   arr_chest = [],
    //   arr_sofa = []
    // this.data.lists.forEach(function(item, index) {
    //   if (item.state == 4) {
    //     arr_chair.push(item)
    //   } else if (item.state == 3) {
    //     arr_tea_table.push(item)
    //   } else if (item.state == 2) {
    //     arr_chest.push(item)
    //   } else {
    //     arr_sofa.push(item)
    //   }
    // })
    // this.setData({
    //   chair: arr_chair
    // })
    // this.setData({
    //   tea_table: arr_tea_table
    // })
    // this.setData({
    //   chest: arr_chest
    // })
    // this.setData({
    //   sofa: arr_sofa
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})